package com.loong.forehead.gateway.common;

import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.StringUtils;

import java.net.InetSocketAddress;

public class RequestUtils {

    /**
     * 获取UID
     *
     * @param request
     * @return
     */
    public String getUid(ServerHttpRequest request) {
        String uid = request.getHeaders().getFirst("uid");
        if (StringUtils.hasText(uid)) {
            return uid;
        }
        return request.getQueryParams().getFirst("uid");
    }

    /**
     * 获取Token
     *
     * @param request
     * @return
     */
    public String getToken(ServerHttpRequest request) {
        String token = request.getHeaders().getFirst("token");
        if (StringUtils.hasText(token)) {
            return token;
        }
        return request.getQueryParams().getFirst("token");
    }

    /**
     * 获取应用类型
     *
     * @param request
     * @return
     */
    public String getAppType(ServerHttpRequest request) {
        HttpHeaders httpHeaders = request.getHeaders();
        String appType = httpHeaders.getFirst("app_type");
        if (StringUtils.isEmpty(appType)) {
            appType = httpHeaders.getFirst("app-type");
        }
        if (StringUtils.isEmpty(appType)) {
            appType = httpHeaders.getFirst("appType");
        }
        if (StringUtils.isEmpty(appType)) {
            appType = request.getQueryParams().getFirst("app_type");
        }
        if (StringUtils.isEmpty(appType)) {
            appType = request.getQueryParams().getFirst("app-type");
        }
        if (StringUtils.isEmpty(appType)) {
            appType = request.getQueryParams().getFirst("appType");
        }
        return appType;
    }

    /**
     * 获取系统类型
     *
     * @param request
     * @return
     */
    public String getOsType(ServerHttpRequest request) {
        HttpHeaders httpHeaders = request.getHeaders();
        String osType = httpHeaders.getFirst("os_type");
        if (StringUtils.isEmpty(osType)) {
            osType = httpHeaders.getFirst("os-type");
        }
        if (StringUtils.isEmpty(osType)) {
            osType = httpHeaders.getFirst("osType");
        }
        if (StringUtils.isEmpty(osType)) {
            osType = request.getQueryParams().getFirst("os_type");
        }
        if (StringUtils.isEmpty(osType)) {
            osType = request.getQueryParams().getFirst("os-type");
        }
        if (StringUtils.isEmpty(osType)) {
            osType = request.getQueryParams().getFirst("osType");
        }
        return osType;
    }

    /**
     * 获取设备号
     *
     * @param request
     * @return
     */
    public String getDeviceId(ServerHttpRequest request) {
        HttpHeaders httpHeaders = request.getHeaders();
        String deviceId = httpHeaders.getFirst("device_id");
        if (StringUtils.isEmpty(deviceId)) {
            deviceId = httpHeaders.getFirst("device-id");
        }
        if (StringUtils.isEmpty(deviceId)) {
            deviceId = httpHeaders.getFirst("deviceId");
        }
        if (StringUtils.isEmpty(deviceId)) {
            deviceId = request.getQueryParams().getFirst("device_id");
        }
        if (StringUtils.isEmpty(deviceId)) {
            deviceId = request.getQueryParams().getFirst("device-id");
        }
        if (StringUtils.isEmpty(deviceId)) {
            deviceId = request.getQueryParams().getFirst("deviceId");
        }
        return deviceId;
    }

    /**
     * 获取请求IP
     *
     * @param request
     * @return
     */
    public String getIp(ServerHttpRequest request) {
        HttpHeaders httpHeaders = request.getHeaders();
        String ip = httpHeaders.getFirst("x-forwarded-for");
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = httpHeaders.getFirst("Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = httpHeaders.getFirst("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            InetSocketAddress inetSocketAddress = request.getRemoteAddress();
            ip = inetSocketAddress.getHostString();
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            return null;
        }
        if (ip.indexOf(",") > -1) {
            return ip.split(",")[0].trim();
        } else {
            return ip;
        }
    }

    private static final RequestUtils instance = new RequestUtils();

    private RequestUtils() {

    }

    public static RequestUtils getInstance() {
        return instance;
    }

}
